package edu.guet.server.mapper;

import edu.guet.server.model.dto.UnitDTO;
import edu.guet.server.model.dto.UnitResourceDTO;
import org.apache.ibatis.annotations.*;
import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import edu.guet.server.model.Unit;

import java.util.List;

@Mapper
public interface UnitMapper {
    Unit findUnit(Unit unit);

    void setUnit(Unit unit);

    void resetUnit(Unit unit);

    /**
     * 获取semester_id对应的单元unit数量
     * @param semester_id
     */
    @Select("select count(*) from unit where semester_id=#{semester_id}")
    Integer countUnit(Integer semester_id);

    /**
     * 添加单元资源
     * @param unitId
     * @param resourceId
     */
    @Insert("INSERT into unit_resource (unit_id, resource_id) VALUES (#{unitId},#{resourceId})")
    void addUnitResource(Integer unitId, Integer resourceId);

    /**
     * 更新单元数量
     *
     * @param unitId
     * @param count
     */
    @Update("update unit set resource_cnt=resource_cnt+#{count} where unit_id=#{unitId}")
    void updateUnitCount(Integer unitId, int count);


    List<UnitResourceDTO> showUnitResouList(Integer unitId, String resourceType);

    List<UnitDTO> findUnits(Integer semesterId);

    @Select("select unit_title from unit where unit_id=#{unitId}")
    String findUnitName(Integer unitId);

    @Select("select unit_id from unit where semester_id=#{semesterId}")
    List<Integer> findSemUnitIds(Integer semesterId);

    @Delete("delete from unit_resource where resource_id=#{resource_id} and unit_id=#{unit_id}")
    void deleteUnitResource(UnitResourceDTO unitResourceDTO);

    @Delete("delete from unit where unit_id=#{unitId}")
    void deleteUnit(Integer unitId);

    @Select("select unit.unit_id,unit_title,resource_cnt from unit where semester_id=#{semesterId}")
    List<Unit> findsemUnits(Integer semesterId);

    List<Unit> findUnitBySemesterId(int id);

    boolean deleteUnitById(int id);

    @Update("update unit set title=#{title} where unit_id=#{unit_id}")
    boolean updateUnitTitle(UnitDTO unitDTO);
}
